package constellation.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;

import androidx.fragment.app.Fragment;

import com.example.myapplication.R;

import java.util.List;
import java.util.Map;

import constellation.Activity.ParntnerInfoActivity;
import constellation.util.AssetsUtil;
import constellation.util.StartInfo;

/**
 * A simple {@link Fragment} subclass.
 */
public class parnterFragment extends Fragment implements AdapterView.OnItemSelectedListener{

    private Spinner spinner1;
    private Spinner spinner2;
    private Button parnternButton;
    Context context;
    View view;
    String[] data;
    ArrayAdapter<String> arrayAdapter;
    private ImageView parnterfragIvMan;
    private ImageView parnterfragIvWoman;
    List<StartInfo.StarinfoBean> list;//存放星座名称的集合
    Map<String, Bitmap> contentlogoImgMap;
    public parnterFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_parnter, container, false);
        context = getContext();
        initView();
        Bundle bundle = getArguments();
        StartInfo starInfo = (StartInfo) bundle.getSerializable("info");
        list= starInfo.getStarinfo();  //数据源
        String logoname=list.get(0).getLogoname();
//        String filename="xzcontentlogo/"+logoname+".png";
//        Bitmap bm= AssetsUtil.getBitmapByAssets(context,filename);
        contentlogoImgMap=   AssetsUtil.getContentlogoImgMap();
        Bitmap bm = contentlogoImgMap.get(logoname);
        parnterfragIvMan.setImageBitmap(bm);
        parnterfragIvWoman.setImageBitmap(bm);
        data = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            data[i] = list.get(i).getName();
        }
        arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, data);
        spinner1.setAdapter(arrayAdapter);
        spinner2.setAdapter(arrayAdapter);
        parnternButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int c1 = spinner1.getSelectedItemPosition();
                int c2= spinner2.getSelectedItemPosition();
                startActivity(new Intent(context, ParntnerInfoActivity.class)
                        .putExtra("man", spinner1.getSelectedItem().toString())
                        .putExtra("woman", spinner2.getSelectedItem().toString())
                .putExtra("men_logo",list.get(c1).getLogoname())
                .putExtra("women_logo",list.get(c2).getLogoname()));


            }
        });
        return view;

    }

    private void initView() {
        spinner1 = (Spinner) view.findViewById(R.id.spinner1);
        spinner2 = (Spinner) view.findViewById(R.id.spinner2);
        parnternButton = (Button) view.findViewById(R.id.parntern_button);
        parnterfragIvMan = (ImageView)view. findViewById(R.id.parnterfrag_iv_man);
        parnterfragIvWoman = (ImageView)view. findViewById(R.id.parnterfrag_iv_woman);
        spinner1.setOnItemSelectedListener(this);
        spinner2.setOnItemSelectedListener(this);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        switch (parent.getId()){
            case R.id.spinner1:
                String logoname=list.get(position).getLogoname();
//                String filename="xzcontentlogo/"+logoname+".png";
//                Bitmap bm= AssetsUtil.getBitmapByAssets(context,filename);
                Bitmap bm = contentlogoImgMap.get(logoname);
                parnterfragIvMan.setImageBitmap(bm);
                break;
            case R.id.spinner2:
                 logoname=list.get(position).getLogoname();
//                 filename="xzcontentlogo/"+logoname+".png";
//                 bm= AssetsUtil.getBitmapByAssets(context,filename);
                bm = contentlogoImgMap.get(logoname);
                parnterfragIvWoman.setImageBitmap(bm);
                break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}
